package databaseConnect;

import java.util.Vector;

public class ScenarioConfigurationUpdateOrInsertProcessor {

	public static boolean setSzenarioParameterRows(Vector<Vector<String>> spr) {
		try {
			int locId = Integer.parseInt(spr.firstElement().get(1));
			Vector<Vector<String>> temp = AccessConnect
					.executeSelect("SELECT COUNT(*) FROM ScenarioDescriptions WHERE ID="
							+ locId);
			if (temp.get(0).get(0).equals("0")) {
				return insertNewParameterRows(spr);
			} else {
				return updateParameterRows(spr, locId);
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public static boolean setHosts(Vector<Vector<String>> hostlist, int id) {
			try {
				AccessConnect.executeQuery(("DELETE FROM StartConfigurationNodes WHERE ScenarioID=" + id));
				Vector<Vector<String>> temp = new Vector<Vector<String>>();
				//System.out.println("hostlist size:" + hostlist.size());
				for (int k = 0 ; k < hostlist.size(); k++){
					for (int i= 0 ; i < hostlist.get(k).size(); i++){
						Vector<String> loc = new Vector<String>();
						loc.add(""+id);
						loc.add(hostlist.get(k).get(i).substring(5));
						loc.add(""+1);
						loc.add(1+"");
						temp.add(loc);
					}					
				}
				return AccessConnect.executeInsert("StartConfigurationNodes", temp);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}		
	}

	public static boolean insertNewParameterRows(Vector<Vector<String>> pr) {
		try {
			return AccessConnect.executeInsert("ScenarioDescriptions", unpivot(pr));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public static boolean updateParameterRows(Vector<Vector<String>> pr,
			int id) {
		try {
			String statement = "UPDATE ScenarioDescriptions SET ";
			for (int i = 1; i < pr.size(); i++) {
				statement = statement + pr.get(i).get(0);
				statement = statement + "='";
				statement = statement + pr.get(i).get(1);
				if (i < pr.size() - 1) {
					statement = statement + "', ";
				} else {
					statement = statement + "' ";
				}
			}
			statement = statement + " WHERE ID=" + id;
			System.out.println(statement);
			AccessConnect.executeQuery(statement);
			return true;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	private static Vector<Vector<String>> unpivot(Vector<Vector<String>> input) {
		Vector<Vector<String>> tmp = new Vector<Vector<String>>();
		Vector<String> content = new Vector<String>();
		for (int i = 0; i < input.size(); i++) {
			content.add(input.get(i).lastElement());
		}
		tmp.add(content);
		return tmp;
	}

	public static boolean setActions(Vector<Vector<String>> input, int scenID){
		try {
			AccessConnect.executeQuery(("DELETE FROM ActionQueue WHERE ScenarioID=" + scenID));
			Vector<Vector<String>> temp = new Vector<Vector<String>>();
			//System.out.println("hostlist size:" + hostlist.size());
			for (int k = 0 ; k < input.size(); k++){
					Vector<String> loc = new Vector<String>();
					loc.add(""+scenID);
					loc.add(input.get(k).get(0));
					loc.add(k+"");
					loc.add(input.get(k).get(1));
					temp.add(loc);					
			}
			return AccessConnect.executeInsert("ActionQueue", temp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}	
	}
}
